<?php
require_once("basepay.class.php");
require_lib('common.function');

class alipay extends BasePay{
	var $Order = null;
	var $MYWWW = '';

	function __construct(){
		$this->MYWWW = get_domain().get_redirect_url(get_app_url('respond/',array('code'=>'alipay')));
		$this->Order = new Order;
	}

	function get_code($order,$payment){
		$charset=$GLOBALS['context']->get_app_conf('charset');
		$real_method=$payment['alipay_pay_method'];
		switch($real_method){
			case '0':
				$service='trade_create_by_buyer';
				break;
			case '1':
				$service='create_partner_trade_by_buyer';
				break;
			case '2':
				$service='create_direct_pay_by_user';
				break;
		}

		$agent='C4335319945672464113';
		$parameter=array(
			'agent'             => $agent,
			'service'           => $service,//"trade_create_by_buyer",
			'partner'           => $payment['alipay_partner'],
			'_input_charset'    => $charset,
			'notify_url'        => $this->MYWWW,
			'return_url'        => $this->MYWWW,
			'subject'           => $order['order_sn'],
			'out_trade_no'      => $order['order_sn'].$order['log_id'],
			'price'             => $order['order_amount'],
			'quantity'          => 1,
			'payment_type'      => 1,
			'logistics_type'    => 'EXPRESS',
			'logistics_fee'     => 0,
			'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',
			'seller_email'      => $payment['alipay_account']
		);
		ksort($parameter);
		reset($parameter);
		$param='';
		$sign='';
		foreach($parameter AS $key => $val){
			$param.="$key=".urlencode($val)."&";
			$sign.="$key=$val&";
		}
		$param=substr($param,0,-1);
		$sign=substr($sign,0,-1).$payment['alipay_key'];
		if($GLOBALS['context']->shop_conf->get('ONLINE_PAY')){
			$button= '<form id="pay_form" name="pay_form" action="https://mapi.alipay.com/gateway.do?'.$param.'&sign='.md5($sign).'&sign_type=MD5" method="post" target="_blank" ></form>';
		}else{
			$button='<div style="text-align:center; margin:0 0 10px 0;"><span class="login_btn"><input id="pay_button" type="button" class="fkbbtn" onclick="window.open(\'https://mapi.alipay.com/gateway.do?'.$param.'&sign='.md5($sign).'&sign_type=MD5\');this.disabled=true;" value="支付宝支付" /></span></div>';
		}
		return $button;
	}

	function respond(){
		$arr=array();
		if(!empty($_POST)){
			foreach($_POST as $key => $data){
				$_GET[$key]=$data;
			}
		}

		$payment = $this->Order->get_payment($_GET['code']);
		$seller_email = rawurldecode($_GET['seller_email']);
		$order_paylog_id = trim(str_replace($_GET['subject'],'',$_GET['out_trade_no']));
//		if(!check_money($order_paylog_id,$_GET['total_fee'])){
//			$arr['respond_flag']='failed';
//			return $arr;
//		}

		$pay_note = '交易号：'.$_REQUEST['trade_no'].'; 付款人:'.$_REQUEST['buyer_email'];
		ksort($_GET);
		reset($_GET);
		$sign='';
		foreach($_GET AS $key=>$val){
			if($key=='sign' || $key=='sign_type' || $key=='code' || $key=='app_act' || $val==''){
				continue;
			}else{
				$sign.="$key=$val&";
			}
		}
		$sign=substr($sign,0,-1).$payment['alipay_key'];

		if(md5($sign)!=$_GET['sign']){
			$arr['respond_flag']='failed';
			return $arr;
		}
		$order_sn = $this->Order->get_orderid_by_paylogid($order_paylog_id);

		/*将回调信息记录入库*/
		$rsp = array();
		foreach($_GET as $k=>$v)
		{
			$rsp[$k]=$v;
		}
		if($order_sn) $this->save_pay_log($order_paylog_id,"alipay", $order_sn, $rsp);
		$pay_amount = $_GET['total_fee'];
		if($_GET['trade_status']=='WAIT_SELLER_SEND_GOODS'){

			//如果买家付款成功，则等待买家发货，调用此接口
			$this->Order->order_paid($order_paylog_id,PS_PAYING,$pay_note,$pay_amount);

			$arr['respond_flag']='success';
			$arr['order_sn']=$order_sn;
			$arr['trade_nu']=$_GET['trade_no'];
			return $arr;
		}elseif($_GET['trade_status']=='TRADE_FINISHED'){
			//如果买家付款成功调用此接口
			$this->Order->order_paid($order_paylog_id,PS_PAYING,$pay_note,$pay_amount);

			$arr['respond_flag']='success';
			$arr['order_sn']=$order_sn;
			$arr['trade_nu']=$_GET['trade_no'];
			return $arr;

		}elseif($_GET['trade_status']=='TRADE_SUCCESS'){
			//如果买家付款成功调用此接口
			$this->Order->order_paid($order_paylog_id,PS_PAYING,$pay_note,$pay_amount);

			$arr['respond_flag']='success';
			$arr['order_sn']=$order_sn;
			$arr['trade_nu']=$_GET['trade_no'];
			return $arr;
		}else{
			$arr['respond_flag']='failed';
			return $arr;
		}
	}
}
?>